<?php
namespace Think;
/**
 * 工具
 * 作为整个项目整合时的一些补充方法
 * 
 * 
 * @author hansir <254103312@qq.com>
 */
class Tools{
	static $Tool; //设置静态属性，供单例化调用
	final public function __construct(){
		//阻止继承和实例化
		if (cookie('issaved') == 'saved') {
			return true;
		}else{
			$ip = get_client_ip();
			$Model = D('visitors');
			$data['v_ip'] = $ip;
			$data['v_it'] = date('Y-m-d H:i:s',time());
			$data['v_url'] = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
			$vr = $Model->add($data);
			//var_dump($vr);exit();
			if ($vr){
				cookie('issaved','saved');
				echo "xxx";exit();
				return true;
			}else{
			echo$Model->getError();
			}
		}
	}
	/**
	 * 单例化方法
	 * 将整个类进行单例化
	 * 
	 * @return 返回实例化Tools类所得到的对象
	 * @author hansir <254103312@qq.com>
	 */
	static public function getIns(){
		if (Tools::$Tool == null) {
			Tools::$Tool = new Tools();
		}
			return Tools::$Tool;  //单例化整个类
	}
	/**
	 * 用户IP
	 * 获得客户端ip并存储到数据库
	 * 
	 * @author 返回true或错误信息
	 */
	public function saveIp(){
		//判断，如果整个IP近期已经记录则返回true
		if (cookie('issaved') == 'saved') {
			return true;
		}else{
			$ip = get_client_ip();
			$Model = D('visitors');
			$data['v_ip'] = $ip;
			$data['v_it'] = date('Y-m-d H:i:s',time());
			$data['v_url'] = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
			$vr = $Model->add($data);
			//var_dump($vr);exit();
			if ($vr){
				var_dump(setcookie('issaved','saved'));
				echo "xxx";exit();
				return true;
			}else{
			echo$Model->getError();
			}
		}
	}
}